package TCPMain;

import static TCPMain.Constant.ACTION_PROGRESS_UPDATE;
import static TCPMain.Constant.ACTION_SendFolder;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.tcp2504.R;

import org.json.JSONObject;

import java.util.Map;

public class DownActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "DownActivity";
    private JSONObject ProgressData=new JSONObject();
    private TCPDownDataAdapter adapter;
    /**
     * Client更新进度条信息-子线程 ok
     * 全部的文件传输Client对象全部保存在service类中，为文件传输Client列表 ok
     * service通过文件传输Client列表获取Client对象->需要在单独的一个线程中获取进度条对象并广播，故为定时线程 ok
     * 获取对象的进度条属性，从而获取到它的进度条对象，然后从这个进度条对象中获取到当前的进度
     * 如果传输完成，Client从文件传输Client列表中请除
     * service通过广播通知activity更新显示
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_down);
        // 初始化UI元素
        Button uploadButton = findViewById(R.id.upload_button);//上传按钮
        Button remoteFilesButton = findViewById(R.id.remote_files_button);//远程文件按钮
        Button transferListButton = findViewById(R.id.transfer_list_button);//传输列表按钮
        Button logoutButton = findViewById(R.id.logout_button);//登出按钮
        uploadButton.setOnClickListener(this);
        remoteFilesButton.setOnClickListener(this);
        transferListButton.setOnClickListener(this);
        logoutButton.setOnClickListener(this);

        RecyclerView recyclerView = findViewById(R.id.file_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this)); // 添加这行        // 注册广播接收器
        LocalBroadcastManager.getInstance(this).registerReceiver(
                progressReceiver,
                new IntentFilter(ACTION_PROGRESS_UPDATE)
        );

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.upload_button) {
            //上传按钮点击事件
            //TODO
        } else if (id == R.id.remote_files_button) {
            //远程文件按钮点击事件
            //TODO
            //还要判断是否已经有数据了，不然会一直跳转到主文件夹
            Intent intent = new Intent(DownActivity.this, ShowDataActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        } else if (id == R.id.transfer_list_button) {
            //传输列表按钮点击事件
            //TODO
        } else if (id == R.id.logout_button) {
            //登出按钮点击事件
            //TODO
            Intent intent1 = new Intent(DownActivity.this, loginActivity.class);
            intent1.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent1);
        } else {
            //其他按钮点击事件
            //TODO
        }

    }

    // 添加广播接收器
    private BroadcastReceiver progressReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ACTION_PROGRESS_UPDATE.equals(intent.getAction())) {
                String data = intent.getStringExtra("data");
                Log.d(TAG, "Received broadcast: " +data);
                /**
                 * 确保 UI 更新在主线程上执行
                 */
                RecyclerView recyclerView = findViewById(R.id.file_list);//文件列表
                if (adapter == null){
                    adapter = new TCPDownDataAdapter(data);
                    recyclerView.setAdapter(adapter);
                }else{
                    adapter.updateData(data);
                }
                runOnUiThread(() -> {
                    adapter.notifyDataSetChanged();
                });
                //runOnUiThread(() -> updateProgressUI(fileName, progress, downloaded, total));
            }
        }
    };
}